package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.tabs.TabLayout;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtSelfPackersManageBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.SelfTeamIntroduceEntity;
import com.sgb.kjwl.view.ui.fragment.self.FragAlbumFragment;
import com.sgb.kjwl.view.ui.fragment.self.FragIntroduceFragment;
import com.sgb.kjwl.view.ui.fragment.self.FragPackersManageFragment;
import com.sgb.kjwl.viewmodel.self.packers.SelfPackersManageModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * @author liwei
 * @date 2020/12/29
 * description:工程工队和工程班组
 */
public class SelfPackersManageActivity extends AppActivity {

    @Inject
    SelfPackersManageModel viewModel;
    private AtSelfPackersManageBinding binding;

    private final String[] tabMenu = {"工队简介", "团队人员", "工种报价", "专业设备", "工程业绩", "我的相册"};
    private SlidingFragmentPagerAdapter mPagerAdapter;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, SelfPackersManageActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().
                appComponent(appComponent).selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        boolean isCompanyRole = !"".equals(companyNo) && !"0".equals(companyNo);
        if (isCompanyRole){
            mActionBar.setTitle("工程工队");
        }else {
            mActionBar.setTitle("工程班组");
        }
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.at_self_packers_manage);
        binding.setViewModel(viewModel);

        ArrayList<Fragment> mFragments = new ArrayList<>();
        for (int i = 0; i <tabMenu.length; i++) {
            if (i == 0) {
                mFragments.add(FragIntroduceFragment.newInstance());
            } else if (i == (tabMenu.length-1)) {
                mFragments.add(FragAlbumFragment.newInstance());
            } else {
                mFragments.add(FragPackersManageFragment.newInstance(i));
            }
        }
        mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, tabMenu);
        binding.vp.setOffscreenPageLimit(tabMenu.length);
        binding.vp.setAdapter(mPagerAdapter);
        binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.tabLayout.setupWithViewPager(binding.vp);
    }

    @Override
    protected void init() {
        viewModel.getTeamIntroduce();
    }

    public void updateIntroduceEntity(SelfTeamIntroduceEntity entity) {
        if (isFinishing()) {
            return;
        }
        if (entity != null) {
            RequestOptions options = new RequestOptions();
            options.placeholder(R.mipmap.packer_avatar);
            options.transforms(new CenterCrop(), new RoundedCorners(8));
            Glide.with(this).load(entity.getAvatarAddress()).apply(options).into(binding.ivAvatar);
            binding.tvTeamName.setVisibility(View.VISIBLE);
            binding.tvTeamAddress.setVisibility(View.VISIBLE);
            binding.tvNotData.setVisibility(View.GONE);
            binding.tvTeamName.setText(entity.getTeamName());
            binding.tvTeamAddress.setText(entity.getAddress());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                // 图片结果回调
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                ((FragAlbumFragment) mPagerAdapter.getItem(5)).pictureSelectorResult(selectList);
            }
        }
    }

    public void refreshData(){
        viewModel.getTeamIntroduce();
    }

    public void refreshTeamData(){
        viewModel.getTeamIntroduce();
        ((FragIntroduceFragment) mPagerAdapter.getItem(0)).refreshTeamData();
    }
}
